package com.kpx.app.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;

import com.kpx.app.R;
import com.journeyapps.barcodescanner.CameraPreview;

/**
 * 作者： Ryan
 * 描述：
 * 创建时间：2015/6/4 16:17.
 */
public class MyScannerView extends View implements Handler.Callback {
    private CameraPreview cameraPreview;
    private float laserScannerY;
    //    private float v0 = 5;
    //    private float a = 1f;
    //    private float time = 1.0f;
    private Bitmap scannerImg;
    private Handler viewHandler = new Handler(this);
    private Thread positionDealThread;
    private boolean doScanning = true;
    private Bitmap scaledBitmap;
    private int laserScannerStopY;
    private int lineWidth;
    private int lineHeight = 4;
    private int laserSpeed;
    private Rect frame;
    private int point1;
    private int point2;
    private int point3;
    private int point4;
    private int point5;
    private int point6;
    private int point7;
    private int point8;
    private Paint paint;

    public MyScannerView(Context context) {
        super(context);
        init();
    }

    public MyScannerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyScannerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {

        positionDealThread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (doScanning) {
                    try {
                        Thread.sleep(100);
                        //                        laserScannerY = laserScannerY + v0 * time + a * time * time / 2;
                        laserScannerY += laserSpeed;
                        viewHandler.sendEmptyMessage(0);
                        //                        time++;
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

    }

    public void stopScanning() {
        doScanning = false;
        laserScannerY = laserScannerStopY;
        postInvalidate();
    }

    public void startScanning() {
        doScanning = true;
        positionDealThread.start();
    }

    public MyScannerView setCameraPreview(CameraPreview cameraPreview) {
        this.cameraPreview = cameraPreview;
        return this;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (cameraPreview == null || cameraPreview.getPreviewFramingRect() == null || cameraPreview.getFramingRect() == null) {
            return;
        }

        if (frame == null) {
            frame = cameraPreview.getFramingRect();
        }
        if (laserScannerStopY == 0) {
            laserScannerStopY = frame.height() / 2 + frame.top;
            laserSpeed = frame.height() / 15;
        }

        if (scannerImg == null) {
            scannerImg = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.scanner);
        }

        if (scaledBitmap == null) {
            scaledBitmap = Bitmap.createScaledBitmap(scannerImg, frame.width(), 5, false);
            scannerImg.recycle();
        }

        if (laserScannerY == 0 || laserScannerY > frame.bottom * 7 / 8) {
            laserScannerY = frame.height() / 5;
            //            time = 1;
        }
        if (paint == null) {
            paint = new Paint();
            paint.setAntiAlias(true);
        }

        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(2);
        paint.setColor(Color.WHITE);
        canvas.drawRect(frame.left, frame.top, frame.right, frame.bottom, paint);
        paint.setColor(getContext().getResources().getColor(R.color.zxing_viewfinder_laser));
        //        canvas.drawRect(frame.left + 20, laserScannerY + 1, frame.right - 20, laserScannerY + 9, paint);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(lineHeight);
        if (lineWidth == 0) {
            lineWidth = frame.width() / 6;
        }
        if (point1 == 0) {
            point1 = frame.left - lineHeight;
            point2 = frame.top - lineHeight;
            point3 = frame.left + lineWidth;
            point4 = frame.right - lineWidth;
            point5 = frame.bottom + lineHeight;
            point6 = frame.right + lineHeight;
            point7 = frame.top + lineWidth;
            point8 = frame.bottom - lineWidth;
        }

        //画四个角的装饰线
        canvas.drawLine(point1, point2, point3, point2, paint);//上左
        canvas.drawLine(point4, point2, point6, point2, paint);//上右
        canvas.drawLine(point1, point2, point1, point7, paint);//左上
        canvas.drawLine(point6, point2, point6, point7, paint);//右上
        canvas.drawLine(point1, point5, point3, point5, paint);//下左
        canvas.drawLine(point4, point5, point6, point5, paint);//下右
        canvas.drawLine(point1, point5, point1, point8, paint);//左下
        canvas.drawLine(point6, point5, point6, point8, paint);//右下
        //画扫描线
        canvas.drawBitmap(scaledBitmap, frame.left, laserScannerY, paint);
    }

    @Override
    public boolean handleMessage(Message message) {
        postInvalidate();
        return false;
    }

}

